package com.jboltai.pptextract;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * Demo class showing how to use the PPTPageProcessor for page-by-page processing
 */
public class PageByPageDemo {
    private static final Logger logger = LoggerFactory.getLogger(PageByPageDemo.class);

    public static void main(String[] args) {
        if (args.length < 2) {
            printUsage();
            System.exit(1);
        }

        String inputFilePath = args[0];
        String outputDirPath = args[1];
        
        File inputFile = new File(inputFilePath);
        File outputDir = new File(outputDirPath);

        if (!inputFile.exists() || !inputFile.isFile()) {
            logger.error("Input file does not exist or is not a file: {}", inputFilePath);
            System.exit(1);
        }

        // Create PPTPageProcessor
        PPTPageProcessor processor = new PPTPageProcessorImpl();

        try {
            // Get slide count
            int slideCount = processor.getSlideCount(inputFile);
            logger.info("Processing presentation with {} slides", slideCount);

            // Process slides one by one
            processor.processSlideBySlide(inputFile, outputDir, new PPTPageProcessor.SlideContentHandler() {
                @Override
                public void onSlideProcessed(int slideNumber, String title, String textContent, Map<String, File> mediaFiles) {
                    logger.info("Processing slide {} of {}", slideNumber, slideCount);
                    
                    // Print slide information
                    System.out.println("\n========== SLIDE " + slideNumber + " ==========");
                    
                    if (title != null && !title.isEmpty()) {
                        System.out.println("Title: " + title);
                    } else {
                        System.out.println("Title: [No title]");
                    }
                    
                    System.out.println("\nText Content:");
                    System.out.println("--------------");
                    if (textContent != null && !textContent.isEmpty()) {
                        System.out.println(textContent);
                    } else {
                        System.out.println("[No text content]");
                    }
                    
                    System.out.println("\nMedia Files:");
                    System.out.println("--------------");
                    if (mediaFiles != null && !mediaFiles.isEmpty()) {
                        for (Map.Entry<String, File> entry : mediaFiles.entrySet()) {
                            System.out.println("- " + entry.getKey() + ": " + entry.getValue().getAbsolutePath());
                        }
                    } else {
                        System.out.println("[No media files]");
                    }
                    
                    System.out.println("==============================\n");
                }
            });
            
            logger.info("Finished processing all slides");
            
        } catch (IOException e) {
            logger.error("Error processing presentation: {}", e.getMessage(), e);
            System.exit(1);
        }
    }

    private static void printUsage() {
        System.out.println("Usage: java -cp <classpath> com.jboltai.pptextract.PageByPageDemo <input-ppt-file> <output-directory>");
        System.out.println("Example: java -cp jboltai_ppt_extract.jar com.jboltai.pptextract.PageByPageDemo presentation.pptx ./output");
    }
} 
